package org.example.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Configuration
@AutoConfigurationPackage
@EnableConfigurationProperties({GirlProperties.class})
@ConditionalOnClass(GirlService.class)
@ConditionalOnProperty(prefix = "girl",name = "name",havingValue = "luse")
public class GirlConfig {

    @Resource
    GirlProperties girlProperties;

    @Bean
    @ConditionalOnMissingBean(GirlService.class)
    public GirlService girlService(){
        return new GirlService(girlProperties);
    }

    @Component
    public class MyBeanPostProcess implements BeanPostProcessor{
        @Override
        @Nullable
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            if(bean instanceof GirlService){
                System.out.println("bean 初始化之前");
            }
            return bean;
        }

        @Override
        @Nullable
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if(bean instanceof GirlService){
                System.out.println("bean 初始化之前");
            }
            return bean;
        }
    }
}
